package com.pospi.pospihold.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lany.sp.SPHelper;
import com.pospi.pospihold.R;
import com.pospi.pospihold.dto.ModifiedDto;
import com.pospi.pospihold.dto.ModifiedGroupDto;
import com.pospi.pospihold.util.Api;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * Created by acer on 2018/9/27.
 */

public class YsAdapter extends BaseAdapter {
    private Context context;
    private List<ModifiedGroupDto> groupDtos;
    private LayoutInflater inflater;
    private String[] mVals = new String[]
            {"Hello", "Android", "Weclome Hi ", "Button", "TextView", "Hello",
                    "Android", "Weclome", "Button ImageView", "TextView", "Helloworld",
                    "Android", "Weclome Hello", "Button Text", "TextView"};
    private List<ModifiedDto> modifiedDtos = new Gson().fromJson(SPHelper.getInstance().getString(Api.YS), new TypeToken<List<ModifiedDto>>() {
    }.getType());
    public YsAdapter(Context context, List<ModifiedGroupDto> groupDtos) {
        this.context = context;
        this.groupDtos = groupDtos;
        inflater = LayoutInflater.from(context);
    }

    @Override

    public int getCount() {
        return groupDtos.size();
    }

    @Override
    public Object getItem(int i) {
        return groupDtos.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_label, null);
            viewHolder = new ViewHolder();
            viewHolder.flowLayout = (TagFlowLayout) view.findViewById(R.id.id_flowlayout);

            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }


        ModifiedGroupDto dto = groupDtos.get(i);
        final List<String> strings = getlabel(dto.getSid());
        viewHolder.flowLayout.setAdapter(new TagAdapter<String>(strings)
        {

            @Override
            public View getView(FlowLayout parent, int position, String s)
            {
                TextView tv = (TextView) inflater.inflate(R.layout.lable_item,
                        viewHolder.flowLayout, false);
                tv.setText(s);
                return tv;
            }
        });
        viewHolder.flowLayout.setOnTagClickListener(new TagFlowLayout.OnTagClickListener() {
            @Override
            public boolean onTagClick(View view, int position, FlowLayout parent) {
                Log.i("tag", strings.get(position));
                return true;
            }
        });


        return view;
    }

    private List<String> getlabel(String id) {
        List<String> strings = new ArrayList<>();
        for (ModifiedDto dto : modifiedDtos) {
            if (dto.getGroupSid().equals(id)) {
                strings.add(dto.getName());
            }
        }
        return strings;
    }

    class ViewHolder {
        TextView no;
        TagFlowLayout flowLayout;
    }
}
